//
//  LJFLeftController.m
//  SevenZhou
//
//  Created by qianfeng on 15/9/15.
//  Copyright (c) 2015年 ljf. All rights reserved.
//

#import "LJFLeftController.h"
#import "LJFLeftCell.h"
#import "LJFContinentModel.h"
#import "LJFLeftModel.h"
#import "Masonry.h"

@interface LJFLeftController ()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView *_tableView;
    NSMutableArray *_tableData;
    
    NSMutableArray *_dataArr;
}

@end

@implementation LJFLeftController

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self)
    {
        _dataArr = [[NSMutableArray alloc] init];
        _tableData = [[NSMutableArray alloc] init];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //self.view.backgroundColor = [UIColor redColor];
    
    [self createTableView];
    
    [self addMyObserver];
}

#pragma mark - 创建tableview

- (void)createTableView
{
    _tableView = [[UITableView alloc] init];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make)
     {
         make.top.equalTo(self.view.mas_top).offset(0);
         make.left.equalTo(self.view.mas_left).offset(0);
         make.bottom.equalTo(self.view.mas_bottom).offset(0);
         make.right.equalTo(self.view.mas_right).offset(0);
     }];
    
    [_tableView registerNib:[UINib nibWithNibName:@"LJFLeftCell" bundle:nil] forCellReuseIdentifier:@"LJFLeftCell"];
}

#pragma mark - 添加观察者

- (void)addMyObserver
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveData:) name:@"SevenZhou" object:nil];
}

- (void)receiveData:(NSNotification *)info
{
    NSArray *arr = info.object;
    
    [_dataArr addObjectsFromArray:arr];
    
    [self loadModel];
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - 加载数据源

- (void)loadModel
{
    NSArray *array1 = @[@"asian.png",@"europe.png",@"oceania.png",@"north_america.png",@"south_america.png",@"africa.png",@"antarctica.png"];
    NSArray *array2 = @[@"asian_black.png",@"europe_black.png",@"oceania_black.png",@"north_america_black.png",@"south_america_black.png",@"africa_black.png",@"antarctica_black.png"];
    
    
    for (int i = 0; i < _dataArr.count; i++)
    {
        LJFContinentModel *m = _dataArr[i];
        
        LJFLeftModel *model = [[LJFLeftModel alloc] init];
        
        model.name = m.name;
        model.imageNomalName = array2[i];
        model.imageSelectName = array1[i];
        if (i == 0)
        {
            model.isSelect = YES;
        }
        else
        {
            model.isSelect = NO;
        }
        
        [_tableData addObject:model];
    }
    
    [_tableView reloadData];
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark - UITableViewDataSource,UITableViewDelegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _tableData.count;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LJFLeftModel *model = _tableData[indexPath.row];

    LJFLeftCell *cell=[tableView dequeueReusableCellWithIdentifier:@"LJFLeftCell"];
    
    cell.NameLabel.text = model.name;
    
    if (model.isSelect)
    {
        cell.iconImage.image = [UIImage imageNamed:model.imageSelectName];
    }
    else
    {
        cell.iconImage.image = [UIImage imageNamed:model.imageNomalName];
    }

    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 85;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    for (LJFLeftModel *model in _tableData)
    {
        model.isSelect = NO;
    }
    
    LJFLeftModel *model = _tableData[indexPath.row];
    model.isSelect = YES;
    
    [_tableView reloadData];
    
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"number" object:@(indexPath.row)];
}


@end
